package com.blog.servlet;

import java.io.IOException;
import java.util.Enumeration;
import java.util.Vector;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.blog.dao.UserDao;
import com.blog.dto.User;
import com.blog.util.DateTime;
import com.blog.util.HardWareUtils;

public class UserServlet extends HttpServlet {
	
	protected boolean login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		boolean flag = false ;
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		UserDao userDao = new UserDao();
		HttpSession session = req.getSession();
		int superstate = userDao.findUserByUsernameAndPassword(username, password);
		if(superstate == 0) {
			return false ;
		}
		else {
			User user = new User();
			user = userDao.findUserByUsername(username).get(0);
			session.setAttribute("user", user);
			session.setAttribute("userSessionID", session.getId());
			System.out.println("sessionID: "+session.getId());
//			req.getRequestDispatcher("/admin/index.jsp").forward(req, resp);
			return true ;
		}
	}
	protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		HttpSession session = req.getSession();
		session.removeAttribute("user");
		session.removeAttribute("userSessionID");
//		resp.sendRedirect(req.getContextPath()+"/admin/login.jsp");
	}
	
	protected void addUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao=new UserDao();
		User u1 = new User();
		u1.setEmail(req.getParameter("email"));
		u1.setPassword(req.getParameter("password"));
		u1.setSuperstate(Integer.parseInt(req.getParameter("superstate")));
		u1.setUsername(req.getParameter("username"));
		u1.setAdd_time(DateTime.getDateTime());
		u1.setLast_login(DateTime.getDateTime());
		u1.setLast_ip(HardWareUtils.getLocalHostAndMac());
		u1.setDelete_at(null);
		userdao.addUser(u1);
		Vector<User> vector = userdao.findAdmin();
		HttpSession session = req.getSession();
		session.setAttribute("adminUser", vector);
//		req.getRequestDispatcher("/admin/admin-list.jsp").forward(req, resp);
		req.getRequestDispatcher("/admin/OKAndClose.html").forward(req, resp);
		return;
	}
	protected void findAllUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao=new UserDao();
		Vector<User> vector = userdao.findAllUser();
		HttpSession session = req.getSession();
		session.setAttribute("allUser", vector);
		return;
	}
	
	protected void findDelUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao=new UserDao();
		Vector<User> vector = userdao.findDelUser();
		HttpSession session = req.getSession();
		session.setAttribute("delUser", vector);
		return;
	}
	protected void findMember(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao=new UserDao();
		Vector<User> vector = userdao.findUserBySuper(1);
		HttpSession session = req.getSession();
		session.setAttribute("memberUser", vector);
		return;
	}
	protected void findAdmin(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao=new UserDao();
		Vector<User> vector = userdao.findAdmin();
		HttpSession session = req.getSession();
		session.setAttribute("adminUser", vector);
		return;
	}
	protected void delUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//		System.out.println("delUser function");
		UserDao userdao = new UserDao();
		userdao.delUserById(Integer.parseInt(req.getParameter("user_id")));
		findAllUser(req, resp);
		return;
	}
	
	protected void hardDelUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao = new UserDao();
		userdao.hardDelUserById(Integer.parseInt(req.getParameter("user_id")));
		findDelUser(req, resp);
		return;
	}
	
	protected void editUser(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		UserDao userdao=new UserDao();
		User u1 = new User();
		u1.setUser_id(Integer.parseInt(req.getParameter("user_id")));
		u1.setEmail(req.getParameter("email"));
		u1.setPassword(req.getParameter("password"));
		u1.setSuperstate(Integer.parseInt(req.getParameter("superstate")));
		u1.setUsername(req.getParameter("username"));
		u1.setLast_ip(req.getParameter("last_ip"));
		userdao.editUser(u1);
		findAllUser(req, resp);
		findAdmin(req, resp);
		findMember(req, resp);
		resp.sendRedirect(req.getContextPath()+"/admin/OKAndClose.html");
//		req.getRequestDispatcher("/admin/admin-list.jsp").forward(req, resp);
		return;
	}
	protected void findUserByUsername(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
		String mintime = req.getParameter("datemin");
		String maxtime = req.getParameter("datemax");
		String username = req.getParameter("username");
		Vector<User> vector = new Vector<User>();
		UserDao userDao =new UserDao();
		vector = userDao.findUserByNameAndTime(mintime, maxtime, username);
		HttpSession session = req.getSession();
		session.setAttribute("findUser", vector);
		resp.sendRedirect(req.getContextPath()+"/admin/admin-find.jsp");
		
	}
	protected void register(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException  {
		UserDao userdao=new UserDao();
		User u1 = new User();
		u1.setEmail(req.getParameter("email"));
		u1.setPassword(req.getParameter("password"));
		u1.setSuperstate(1);
		u1.setUsername(req.getParameter("username"));
		u1.setAdd_time(DateTime.getDateTime());
		u1.setLast_login(DateTime.getDateTime());
		u1.setLast_ip(HardWareUtils.getLocalHostAndMac());
		u1.setDelete_at(null);
//		System.out.println(u1.getEmail()+"  "+u1.getUsername());
		if(!userdao.addUser(u1)) {
			resp.sendRedirect(req.getContextPath()+"/home/registerError.html");
			return ;
		}else {
			findMember(req,resp);
			resp.sendRedirect(req.getContextPath()+"/home/registerOK.html");
			return ;
		}
		
	}
	
	
	
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
//		System.out.println("111111111");
		//System.out.println(req.getParameterNames());查找所有请求的的参数
//		Enumeration<String> enum1 = req.getParameterNames();
//		while(enum1.hasMoreElements()){ 
//			String paraName = (String) enum1.nextElement(); 
//			if(paraName.equals("act")) {
//				System.out.println("222222");
//				act=req.getParameter("act");
//				break;
//			}
//		}
		String act = null;
		act=req.getParameter("act");
		
		if(act==null || act.equals("")) act = "index";
		System.out.println("UserServlet act = "+act);
		
		switch (act) {
		case "add":
			addUser(req, resp);
			break;
		case "del":
			delUser(req, resp);
			findAdmin(req, resp);
			findMember(req, resp);
			break;
		case "hardDel":
			hardDelUser(req, resp);
			break;
		case "edit":
			editUser(req, resp);
			break;
		case "findDelUser":
			findDelUser(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/member-del.jsp");
			break;
		case "adminList":
			findAdmin(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/admin-list.jsp");
			break;
		case "memberList":
			findMember(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/member-list.jsp");
			break;
		case "login":
			if(!login(req, resp)){
				req.getRequestDispatcher("/home/error.html").forward(req, resp);
				break;
			}
			resp.sendRedirect(req.getContextPath()+"/home/index");
			break;
		case "AdminLogin":
			if(!login(req, resp)) {
				req.getRequestDispatcher("/admin/error.html").forward(req, resp);
				break;
			}
			resp.sendRedirect(req.getContextPath()+"/admin/index.jsp");
			break;
		case "logout":
			logout(req, resp);
			resp.sendRedirect(req.getContextPath()+"/home/login.jsp");
			break;
		case "AdminLogout":
			logout(req, resp);
			resp.sendRedirect(req.getContextPath()+"/admin/login.jsp");
			break;
		case "register":
			register(req, resp);
			break;
			

		default:
			findAllUser(req, resp);
//			页面重定向
			resp.sendRedirect(req.getContextPath()+"/index.jsp");
//			页面转发
//			req.getRequestDispatcher("/index.jsp").forward(req, resp);
			break;
				
		}
		
		
		
	}

}
